package org.cloud.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.cloud.pojo.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ResultJsonUtils {
    public static void parseResult(String pmsPath,String shopPath){

// 读取pms json文件
        JSONObject pmsObejct = readJsonFile(pmsPath);

// 读取微店json文件
        JSONObject shopObejct = readJsonFile(shopPath);

        RoomTypeArray roomTypeArray = pmsObejct.toJavaObject(RoomTypeArray.class);

        ResultJsonArray result = shopObejct.toJavaObject(ResultJsonArray.class);

//        String shop = shopObejct.getString("units");
//
//        ResultJsonArray resultJsonArray = JSON.parseObject(shop, ResultJsonArray.class);
//
//        String room = pmsObejct.getString("rooms");
//
//        RoomTypeArray roomTypeArray = JSON.parseObject(room, RoomTypeArray.class);

        UnitJsonArray unitJsonArray = generateResult(result, roomTypeArray);
        String jsonString = JSON.toJSONString(unitJsonArray);
        System.out.println(jsonString);
    }

    public static JSONObject readJsonFile(String filename){
        String jsonString = "";
        File jsonFile = new File(filename);
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer stringBuffer = new StringBuffer();
            while ((ch = reader.read()) != -1){
                stringBuffer.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonString = stringBuffer.toString();
        } catch (FileNotFoundException e){
            JSONObject notFoundJson = new JSONObject();
            notFoundJson.put("msg","该地区GeoJson文件不存在！");
            return notFoundJson;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(jsonString);
    }

    public static UnitJsonArray generateResult(ResultJsonArray resultJsonArray,RoomTypeArray roomTypeArray){

        ArrayList<UnitJson> list=new ArrayList<>();


        ArrayList<ResultJson> results = resultJsonArray.getUnits();

        ArrayList<RoomType> rooms = roomTypeArray.getRooms();




        for (ResultJson resultJson : results){

            int currentMaxNum = 0;
            String currentMaxId = "none";
             for (RoomType room : rooms){
                 String roomTypeName = room.getRoomTypeName();
                 String id = room.getRoomTypeId();
                 int lcs = LCS(resultJson.getChannelUnitName(),roomTypeName);
                 if (lcs >currentMaxNum){
                     currentMaxNum = lcs;
                     currentMaxId = id;
                 }
             }
            UnitJson unitJson = new UnitJson(resultJson.getId(),resultJson.getChannelUnitName(),currentMaxId);
             list.add(unitJson);
        }

        UnitJsonArray unitJsonArray = new UnitJsonArray();

        unitJsonArray.setUnits(list);

        return unitJsonArray;
    }

    public static int LCS(String x,String y){
        char[] a = x.toCharArray();
        char[] b = y.toCharArray();
        int m = a.length;
        int n = b.length;
        int max = 0;
//        int index = 0;
        int[][] p = new int[m+1][n+1];

        for (int i = 0; i <= m; i++) {
            p[i][0] = 0;
        }

        for (int i = 0; i <= n; i++) {
            p[0][n] = 0;
        }


        for (int i = 1; i < m+1; i++) {
            for (int j = 1; j < n+1; j++) {
                if (a[i-1]==b[j-1]){
                    p[i][j] = p[i-1][j-1]+1;

                    if (p[i][j] > max){
                        max = p[i][j];
//                        index = i;
                    }
                }else {
                    p[i][j] = 0;
                }
            }
        }

//        for (int i = max; i > 0; i--) {
//            System.out.print(a[index-i]);
//        }
//        System.out.println();
        return max;
    }
}
